package com.lz.football.module.sign.view
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.LoginEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.message.http.JsClient;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.login.ILoginSignView;
	import com.lz.football.module.sign.item.LoginSignAwardItem;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.core.TitleWindow;
	import lz.controls.core.UIComponent;

	/**
	 *登录签到 
	 * @author lake
	 * 
	 */	
	public class LoginSignView extends TitleWindow implements ILoginSignView
	{
		/**
		 *关闭按钮 
		 */		
		public static var CLoseBtn	 	:SimpleButton;
		
		private var loader 			:QueueLoader;//加载器
		/**
		 *签到按钮 
		 */		
		public static var signBtn		:SimpleButton;
		/**
		 *补签按钮 
		 */		
		private var reSignBtn			:SimpleButton;
		/**
		 *补签费用 
		 */		
		private var txtReRice			:TextField;
		/**
		 *补签费用 静态描述 
		 */		
		private var reSignTxtMc		:MovieClip;
		/**
		 *已签到标签 
		 */		
		private var signMc				:MovieClip;
		/**
		 *黄钻标识 
		 */		
		private var qzoneTagMc			:MovieClip;
		/**
		 *日历面板 
		 */		
		private var calendarPanel		:CalendarPanel;
		/**
		 *奖励条目列表 
		 */		
		private var awardItems 		:Array = [];
		/**
		 *数据 
		 */		
		private var _data :*;
		
		public function LoginSignView()
		{
			super();
		}
		/**
		 * 设置数据
		 * @param value
		 * 
		 */		
		public function set data(value:*):void
		{
			_data = value;
		}
		/**
		 * 获取数据
		 * @return 
		 * 
		 */		
		public function get data():*
		{
			return _data;
		}
		/**
		 * 初始化
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.lz.res.LoginSignView");
			//初始化宽高
			width = 587;
			height = 339;
			if(_symbolSkin)
			{
				config();
			}
			else
			{
				loader = new QueueLoader();
				loader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,loadCompleteHandler);
				loader.addQueue(new QueueVo("LoginSign"),"res/swf/LoginSign/LoginSign.swf?" + GlobalDef.ReleaseVersion + ".swf");
				loader.queueLoadStart();
			}
		}
		/**
		 * 配置
		 * 
		 */		
		private function config():void
		{
			this.y -= 20;
			_symbolSkin.alpha = .2;
			TweenLite.to(_symbolSkin,.5,{alpha:1});
			this.addChild(_symbolSkin);
			closeBtn = Reflection.createInstance("com.lz.commonUI.closeBtn");
			this.addChild(closeBtn);
			closeBtn.x = 578;
			closeBtn.y = 4;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			//设置可拖动
			dragEnabled = true;
			//设置关闭按钮
			setCloseBtn(closeBtn);
			
			calendarPanel = new CalendarPanel();
			calendarPanel.initUI(_symbolSkin.calendarPanel);
			calendarPanel.data = _data;
			signBtn = _symbolSkin.signBtn;
			reSignBtn = _symbolSkin.reSignBtn;
			txtReRice = _symbolSkin.txtReRice;
			reSignTxtMc = _symbolSkin.reSignTxtMc;
			signMc = _symbolSkin.signMc;
			qzoneTagMc = _symbolSkin.qzoneTagMc;
			if(_data.is_sign)
			{
				signBtn.visible = false;
			}
			else
			{
				signMc.visible = false;
			}
			if(_data.is_sign && _data.sign_days<_data.signDayTags.length)//本月有补签
			{
				reSignBtn.visible = true;
				txtReRice.visible = true;
				reSignTxtMc.visible = true;
				txtReRice.text = "" + _data.resignRice;
			}
			else
			{
				reSignBtn.visible = false;
				txtReRice.visible = false;
				reSignTxtMc.visible = false;
				txtReRice.text = "0";
			}
			var i :int = 0;
			var awardItem :LoginSignAwardItem;
			awardItem = new LoginSignAwardItem();
			awardItem.initUI(_symbolSkin["item_" + 0]);
			awardItem.awardType = 0;
			awardItem.data = _data;
			awardItems.push(awardItem);
			
			awardItem = new LoginSignAwardItem();
			awardItem.initUI(_symbolSkin["item_" + 1]);
			var getDayFlags :Array = AppData.getInstance().user.signInfoVo.getDayFlags;
			var days :Array = [5,12,26];
			i = 0;
			awardItem.awardType = 3;
			awardItem.awardDays = 26;
			for(;i<getDayFlags.length;i++)
			{
				if(getDayFlags[i] == 0 )
				{
					awardItem.awardType = i+1;
					awardItem.awardDays = days[i];
					break;
				}
			}
			awardItem.data = _data;
			awardItems.push(awardItem);
			/*var i :int;
			var awardItem :LoginSignAwardItem;
			for(i = 0;i < 4;i++)
			{
				awardItem = new LoginSignAwardItem();
				awardItem.initUI(_symbolSkin["item_" + i]);
				awardItem.awardType = i;
				if(i == 1)
				{
					awardItem.awardDays = 5;
				}
				if(i == 2)
				{
					awardItem.awardDays = 12;
				}
				if(i == 3)
				{
					awardItem.awardDays = 26;
				}
				awardItem.data = _data;
				awardItems.push(awardItem);
			}*/
			if(AppData.getInstance().user.userDetailInfo.isQzoneVip())
			{
				qzoneTagMc.gotoAndStop(2);
			}
			addEvtListener();
			//@lake 2013-11-16 Guide use
			CLoseBtn = closeBtn;
		}
		/**
		 * loaderErrorHandler
		 * @param e
		 * 
		 */		
		private function loaderErrorHandler(e :Event):void
		{
			
		}
		/**
		 * 
		 * 加载完毕
		 */		
		private function loadCompleteHandler(e :Event):void
		{
			_symbolSkin = Reflection.createInstance("com.lz.res.LoginSignView");
			if(_symbolSkin)
			{
				config();
				//@lake 2013-12-05 Guide use
				AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MODULE_LOAD_RES_COMPLETE);
			}
			else
			{
				destroyView();
			}
			loader.removeEventListener(QueueLoaderEvent.QUEUE_COMPLETE,loadCompleteHandler);
			loader = null;
		}
		/**
		 * 
		 * 添加事件
		 */		
		public function addEvtListener():void
		{
			signBtn.addEventListener(MouseEvent.CLICK,onSignHandler);
			reSignBtn.addEventListener(MouseEvent.CLICK,reSignHandler);
			AppData.getInstance().user.signInfoVo.addEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *数据变化 
		 * @param e
		 * 
		 */		
		private function dataChangeHandler(e :Event):void
		{
			if(_data.is_sign)
			{
				signBtn.visible = false;
				signMc.visible = true;
			}
			else
			{
				signMc.visible = false;
				signBtn.visible = true;
			}
			if(_data.is_sign && _data.sign_days<_data.signDayTags.length)//本月有补签
			{
				reSignBtn.visible = true;
				txtReRice.visible = true;
				reSignTxtMc.visible = true;
				txtReRice.text = "" + _data.resignRice;
			}
			else
			{
				reSignBtn.visible = false;
				txtReRice.visible = false;
				reSignTxtMc.visible = false;
				txtReRice.text = "0";
			}
			calendarPanel.updateData();
			awardItem = awardItems[1];
			var getDayFlags :Array = AppData.getInstance().user.signInfoVo.getDayFlags;
			var days :Array = [5,12,26];
			var i :int = 0;
			for(;i<3;i++)
			{
				if(getDayFlags[i] == 0 || getDayFlags[i]==2)
				{
					awardItem.awardType = i+1;
					awardItem.awardDays = days[i];
					break;
				}
			}
			for each(var awardItem :LoginSignAwardItem in awardItems)
			{
				awardItem.updateData();
			}
		}
		/**
		 * 
		 * 删除事件
		 */		
		public function removeEvtListener():void
		{
			signBtn.removeEventListener(MouseEvent.CLICK,onSignHandler);
			reSignBtn.removeEventListener(MouseEvent.CLICK,reSignHandler);
			AppData.getInstance().user.signInfoVo.removeEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 * onCloseHandler
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			//@lake 2013-04-15
			AppEventDispatcher.getInstance().dispatchEvent(new Event("Guide_Sign_View_Remove",true));
			destroyView();
		}
		/**
		 *开通黄砖 
		 * @param e
		 * 
		 */		
		private function openHuangHandler(e :MouseEvent):void
		{
			JsClient.app_tobemember(1);
		}
		/**
		 *签到 
		 * @param e
		 * 
		 */		
		private function onSignHandler(e :MouseEvent):void
		{
			//signBtn.mouseEnabled = false;
			AppEventDispatcher.getInstance().dispatchEvent(new LoginEvent(LoginEvent.USER_SIGN));
		}
		/**
		 *补签 
		 * @param e
		 * 
		 */		
		private function reSignHandler(e :MouseEvent):void
		{
			var rice :int = _data.resignRice;
			var cashpoint :int = AppData.getInstance().clubDatamanager.clubInfo.cashpoint
			var info :String;
			if(rice>cashpoint)
			{
				info = "消耗"+rice+"点卷可补签。现有点券不足，是否前往充值?";
				Alert.noCashAlert(info);
				return;
			}
			info = "补签需要" + rice + "点券,是否补签?";
			Alert.show(info,"",Alert.YES|Alert.NO,GlobalDef.GameStage,resignAlertHandler);
		}
		/**
		 *补签确认 
		 * @param type
		 * 
		 */		
		private function resignAlertHandler(type:int):void
		{
			switch(type)
			{
				case Alert.YES:
				{
					AppEventDispatcher.getInstance().dispatchEvent(new LoginEvent(LoginEvent.USER_RESIGN));
					break;
				}
			}
		}
		/**
		 *活动日历 
		 * @param e
		 * 
		 */		
		private function onActivityHandler(e :MouseEvent):void
		{
			Alert.showErrorTip("此功能尚未开放!");
		}
		/**
		 * 销毁
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			TweenLite.to(this,.3,{alpha:0,onComplete:fadeOutComplete});
			_data = null;
			signBtn = null;
			CLoseBtn = null;
		}
		/**
		 *消失 
		 * 
		 */		
		override protected function fadeOutComplete():void
		{
			calendarPanel.destroyView();
			calendarPanel = null;
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}